﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Douban.Api.User;
using Douban.Core;
using Microsoft.Practices.Composite.Presentation.Commands;
using Douban.Core.Settings;
using Douban.Core.Menus;
using System.Collections.ObjectModel;
using Douban.Core.HotKeys;
using Microsoft.Practices.Composite.Events;
using Douban.Core.Events;
using Microsoft.Practices.ServiceLocation;

namespace Douban
{
    public class ShellViewModel : ObservableObject, IShellViewModel
    {
        private ICommand _closeCommand;
        private ICommand _minimumCommand;
        private ICommand _openSettingWindowCommand;
        private ICommand _notifyIconDoubleClickCommand;
        private IMenuManager _menuManager;
        private IEventAggregator _eventAggregator;
        private byte _windowOpacity;
        private ObservableCollection<MenuItem> _menus;

        public ShellViewModel(IShellView view, IMenuManager menuManager, IEventAggregator eventAggregator)
        {
            View = view;
            View.Context = this;
            _menuManager = menuManager;
            _menuManager.MenuChanged += new MenuChangedEventHandler(OnMenuChanged);
            _menus = new ObservableCollection<MenuItem>(menuManager.GetMenuItems());

            _eventAggregator = eventAggregator;

            RegisterHotKeyHandler();
        }

        private void RegisterHotKeyHandler()
        {
        }

        private void OnMenuChanged(object sender, MenuChangedEventArgs e)
        {
            switch(e.MenuAction)
            {
                case MenuAction.Add:
                    _menus.Add(e.Menu);
                    break;
                case MenuAction.Remove:
                    _menus.Remove(e.Menu);
                    break;
                default:
                    break;
            }
        }

        public IShellView View { get; set; }

        public byte WindowOpacity 
        {
            get { return _windowOpacity; }
            set
            {
                if (_windowOpacity != value)
                {
                    _windowOpacity = value;
                    NotifyPropertyChanged("WindowOpacity");
                }
            }
        }

        public ObservableCollection<MenuItem> Menus
        {
            get { return _menus; }
            set
            {
                if (_menus != value)
                {
                    _menus = value;
                    NotifyPropertyChanged("Menus");
                }
            }
        }

        public NotifyIconViewModel NotifyIconDataContext
        {
            get { return ServiceLocator.Current.GetInstance<NotifyIconViewModel>(); }
        }

        public ICommand CloseCommand
        {
            get
            {
                if(_closeCommand == null)
                    _closeCommand = new DelegateCommand<object>(obj => View.CloseView());

                return _closeCommand;
            }
        }

        public ICommand MinimumCommand
        {
            get
            {
                if (_minimumCommand == null)
                    _minimumCommand = new DelegateCommand<object>(obj => View.Minimum());

                return _minimumCommand;
            }
        }

        public ICommand OpenSettingWindowCommand
        {
            get
            {
                if (_openSettingWindowCommand == null)
                    _openSettingWindowCommand = new DelegateCommand<object>(
                        obj =>
                        {
                            if (!SettingWindowHelper.IsOpen)
                            {
                                SettingWindowHelper.IsOpen = true;
                                ServiceLocator.Current.GetInstance<ISettingViewModel>().Open();
                            }
                        });

                return _openSettingWindowCommand;
            }
        }
    }
}
